using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Projektet.Controller
{
    public enum GameState 
    {
        InGame,
        InMenu,
        InUpgradeMenu,
        InInfo,
        Gameover,
        VictoryScreen
    }

    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MasterController : Microsoft.Xna.Framework.Game, Model.EventListener
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private GameState _gameState;

        KeyboardState _oldState;

        GameController _gameController;
        MenuController _menuController;
        UpgradeController _upgradeController;

        private int _levelCount;

        public MasterController()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            _gameState = GameState.InMenu;
            _levelCount = 1;
            _gameController = new GameController(this, _levelCount);
            _menuController = new MenuController(_levelCount);
            _upgradeController = new UpgradeController(_gameController._gameModel._humanPlayer);

            int resolutionX = 960;
            int resolutionY = 720;
            View.Camera.Resolution = new Vector2(resolutionX, resolutionY);
            SetResolution(resolutionX, resolutionY);
            //View.Camera camera = new View.Camera(resolutionX, resolutionY);
        }
        
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            _gameController.LoadContent(Content);
            _menuController.LoadContent(Content);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            CheckForInput();
            if (_gameState == GameState.InGame)
            {            
                _gameController.Update(gameTime);
            }
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            _gameController.Draw(spriteBatch);

            if (_gameState == GameState.InMenu || _gameState == GameState.InUpgradeMenu || _gameState == GameState.Gameover || _gameState == GameState.VictoryScreen || _gameState == GameState.InInfo)
            {
                _menuController.Draw(spriteBatch, _gameState);
            }
            // TODO: Add your drawing code here

            base.Draw(gameTime);
            spriteBatch.End();
        }

        private void CheckForInput() 
        {
            KeyboardState newState = Keyboard.GetState();
            if(_gameState == GameState.Gameover || _gameState == GameState.VictoryScreen)
            {
                if (newState.IsKeyUp(Keys.R) && _oldState.IsKeyDown(Keys.R))
                {
                    RestartGame();
                }
            }
            else
            {
                //Pause
                if (newState.IsKeyUp(Keys.Escape) && _oldState.IsKeyDown(Keys.Escape))
                {
                    if (_gameState == GameState.InGame)
                    {
                        _gameState = GameState.InMenu;
                    }
                    else 
                    {
                        _gameState = GameState.InGame;
                    }
                }
                //Info menu
                if(_gameState == GameState.InGame || _gameState == GameState.InInfo)
                {
                    if (newState.IsKeyUp(Keys.I) && _oldState.IsKeyDown(Keys.I))
                    {                    
                        if(_gameState == GameState.InGame)
                        {
                            _gameState = GameState.InInfo;
                            Console.WriteLine("Info!");
                        }
                        else 
                        {
                            _gameState = GameState.InGame;
                        }
                    }
                }
                //Upgrade menu
                if(_levelCount > 2)
                {
                    if(_gameState == GameState.InGame ||_gameState == GameState.InUpgradeMenu)
                    {
                        if(_gameState == GameState.InUpgradeMenu)
                        {
                            _upgradeController.CheckForUpgradeInput(newState, _oldState);
                        }                    
                        if (!(_gameState == GameState.InUpgradeMenu) && (newState.IsKeyUp(Keys.U) && _oldState.IsKeyDown(Keys.U)))
                        {
                            //_gameController._gameModel._humanPlayer._unitStats.ArcherAttackSpeed = 10f;
                            Console.WriteLine("Uppgraderingar!");
                            _gameState = GameState.InUpgradeMenu;
                        }
                        else if (_gameState == GameState.InUpgradeMenu && (newState.IsKeyUp(Keys.U) && _oldState.IsKeyDown(Keys.U)))
                        {
                            _gameState = GameState.InGame;
                        }                    
                    }
                }
            
                if(_gameState == GameState.InGame)
                {
                    //Spawn
                    if (newState.IsKeyUp(Keys.D1) && _oldState.IsKeyDown(Keys.D1))
                    {
                        _gameController._gameModel._humanPlayer.AddPikeman();
                    }
                    if (newState.IsKeyUp(Keys.D2) && _oldState.IsKeyDown(Keys.D2))
                    {
                        _gameController._gameModel._humanPlayer.AddArcher();
                    }
                    if (newState.IsKeyUp(Keys.D3) && _oldState.IsKeyDown(Keys.D3))
                    {
                        _gameController._gameModel._humanPlayer.AddKnight();
                    }                
                    //CameraScroll
                    if (newState.IsKeyDown(Keys.Left))
                    {
                        View.Camera.DecreaseOffset();
                    }
                    if (newState.IsKeyDown(Keys.Right))
                    {
                        View.Camera.IncreaseOffset();
                    }
                    //Catapult
                    if(_levelCount > 1)
                    {
                        if (newState.IsKeyUp(Keys.Space) && _oldState.IsKeyDown(Keys.Space))
                        {
                            _gameController._gameModel._humanPlayer._catapult.Shoot();
                        }
                        if (newState.IsKeyDown(Keys.Q))
                        {
                            _gameController._gameModel._humanPlayer._catapult.DecreasePower();
                        }
                        if (newState.IsKeyDown(Keys.W))
                        {
                            _gameController._gameModel._humanPlayer._catapult.IncreasePower();
                        }
                    }
                }
            }

            _oldState = newState;
        }

        public void EventHandler(Model.Event activeEvent) 
        {
            switch(activeEvent)
            {
                case Model.Event.PlayerVictory:
                    //Console.WriteLine("Du vann!");
                    if(_levelCount >= 3)
                    {
                        _gameState = GameState.VictoryScreen;
                    }
                    else
                    {
                        NewLevel();
                    }
                    break;
                case Model.Event.NpcVictory:
                    _gameState = GameState.Gameover;
                    break;
            }
        }

        private void RestartGame() 
        {
            _levelCount = 1;
            _gameController = new GameController(this, _levelCount);
            _menuController = new MenuController(_levelCount);
            _upgradeController = new UpgradeController(_gameController._gameModel._humanPlayer);
            LoadContent();
            _gameState = GameState.InMenu;
        }

        private void NewLevel() 
        {
            if(_levelCount < 3)
            {
                _levelCount++;
                _gameController = new GameController(this, _levelCount);
                _menuController = new MenuController(_levelCount);
                _upgradeController = new UpgradeController(_gameController._gameModel._humanPlayer);
                LoadContent();
                _gameState = GameState.InMenu;
            }
            //else if(_levelCount > 3)
            //{
            //    Console.WriteLine("Du vann!");
            //}
        }

        private void SetResolution(int resX, int resY) 
        {
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferHeight = resY;
            graphics.PreferredBackBufferWidth = resX;
            this.IsMouseVisible = true;
            this.Window.Title = "Krossa Borgarna";
        }
    }
}
